<?php
/**
 * Created by PhpStorm.
 * User: vanity
 * Date: 2018/1/3
 * Time: 16:20
 */
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController
{
    protected $fields = [
        'name' => '',
        'image' => '',
        'password' => '',
    ];
    public function index()
    {
        $user = D('user')->relation('role_user')->select();
        $this->assign('user',$user);
        $this->display();
    }
    public function create()
    {
        $data = [];
        foreach($this->fields as $field => $value){
            $data[$field] = $value;
        }
        $this->assign('data',$data);
        $this->display();
    }
    public function store()
    {
        if(empty($_POST['password'])){
            $this->withError('密码不能为空！',U('admin/user/create'));
        }
        $user = D('user');
        $data = $user->create();
        if(!$data){
            $this->withError($user->getError(),U('admin/user/create'));
        }
        if($_FILES['image']['error'] == 0){
            $file = $this->upload($_FILES);
            if($file['status'] != 1){
                $this->withError($file['msg'],U('admin/user/create'));
            }
            $data['image'] = $file['image']['savepath'].$file['image']['savename'];
        }
        if(!empty($_POST['role_id'])){
            $data['role_user'] = array('role_id'=>$_POST['role_id']);
            $res = $user->relation('role_user')->data($data)->add();
        }else{
            $res = $user->data($data)->add();
        }
        if(!$res){
            $this->withError('添加失败',U('admin/user/create'));
        }
        $this->withSuccess('添加成功',U('admin/user/index'));
    }
    public function edit()
    {
        $user = D('user')->where(array('id'=>intval($_REQUEST['id'])))->relation('role_user')->find();
        foreach(array_keys($this->fields) as $field) {
            $data[$field] = $user[$field];
        }
        $data['id'] = $user['id'];
        $data['role_id'] = $user['role_id'];
        $this->assign('data',$data);
        $this->display();
    }
    public function update()
    {
        $user = D('user');
        $data = $user->create();
        if(empty($_POST['password'])){
            unset($data['password']);
        }
        if(!$data){
            $this->withError($user->getError(),U('admin/user/index'));
        }
        if($_FILES['image']['error'] == 0){
            $file = $this->upload($_FILES);
            if($file['status'] != 1){
                $this->withError($file['msg'],U('admin/user/edit',array('id'=>$_POST['id'])));
            }
            $data['image'] = $file['image']['savepath'].$file['image']['savename'];
        }
        $res = $user->save($data);
        if(!$res){
            $this->withError('修改失败',U('admin/user/edit',array('id'=>$_POST['id'])));
        }
        //用户组修改
        $role = D('RoleUser')->changeRole($_POST['id'],$_POST['role_id']);
        if(empty($role)){
            $this->withError('角色设置失败',U('admin/user/index'));
        }
        $this->withSuccess('修改成功',U('admin/user/index'));
    }
    public function delete()
    {
        $id = intval($_POST['id']);
        $url = U('admin/user/index');
        if($id == ''|| $id == 0){
            $this->withError('信息丢失！',$url);
        }elseif($id == 1){
            $this->withError('权限不足！',$url);
        }
        $user = D('User');
        $map['id'] = $id;
        $result = $user->where($map)->relation('role_user')->delete();
        if(!$result){
            $this->withError('删除失败',$url);
        }
        $this->withSuccess('删除成功',$url);
    }

}